home *** CD-ROM | disk | FTP | other *** search
- /*
- ** This source code was written by Tim Endres
- ** Email: time@ice.com.
- ** USMail: 8840 Main Street, Whitmore Lake, MI 48189
- **
- ** Some portions of this application utilize sources
- ** that are copyrighted by ICE Engineering, Inc., and
- ** ICE Engineering retains all rights to those sources.
- **
- ** Neither ICE Engineering, Inc., nor Tim Endres,
- ** warrants this source code for any reason, and neither
- ** party assumes any responsbility for the use of these
- ** sources, libraries, or applications. The user of these
- ** sources and binaries assumes all responsbilities for
- ** any resulting consequences.
- */
-
- #ifdef MPW3
- # pragma segment MACBIN
- #endif
-
- #ifndef THINK_C_PRECOMPILED
-
- #include <types.h>
- #include <resources.h>
- #include <toolutils.h>
- #include <standardfile.h>
-
- #include <fcntl.h>
- #include <stdio.h>
-
- #include "defines.h"
-
- #endif /*THINK_C_PRECOMPILED*/
-
- #include <errno.h>
- #include "asd.h"
- #include "mb.h"
-
-
- #undef BLK_SIZE
- #define BLK_SIZE 1024
- #define iobuffer kBuffer
- extern char kBuffer[];
- /* static char iobuffer[BLK_SIZE]; */
-
- #ifdef TCLAPPL
-
- SF_asd_to_mb()
- {
- Point mypoint;
- SFReply asdreply;
- SFReply mbreply;
- SFTypeList mytypes;
- FILE *mbfile;
- FILE *asdfile;
-
- mypoint.h = mypoint.v = 75;
- mytypes[0] = 'TEXT';
- MyGetFile(mypoint, "\p", NULL, (CheckOption()?-1:1), mytypes, NULL, &asdreply);
- if (asdreply.good) {
- MyPutFile(mypoint, "\pMacBinary Filename:", "\pfile.mb", NULL, &mbreply);
- if (mbreply.good) {
- p2cstr(mbreply.fName);
- p2cstr(asdreply.fName);
- Feedback("Apple Single/Double to MacBinary '%s' -> '%s'\n",
- asdreply.fName, mbreply.fName);
- SetVol(NULL, asdreply.vRefNum);
- asdfile = fopen(asdreply.fName, "r");
- if (asdfile == NULL) {
- Feedback("Error #%d opening '%s'", errno, asdreply.fName);
- return FAILURE;
- }
- SetVol(NULL, mbreply.vRefNum);
- mbfile = fopen(mbreply.fName, "w");
- if (mbfile == NULL) {
- Feedback("Error #%d opening '%s'", errno, mbreply.fName);
- fclose(asdfile);
- return FAILURE;
- }
-
- yield_menus(YIELD_ON);
-
- do_asd_to_mb(asdreply.fName, asdfile, mbreply.fName, mbfile);
-
- WatchCursorOn();
- fclose(asdfile);
- fclose(mbfile);
-
- yield_menus(YIELD_OFF);
- UInitCursor();
- }
- }
- }
-
- #endif
-
- do_asd_to_mb(inname, infile, outname, outfile)
- char *inname;
- FILE *infile;
- char *outname;
- FILE *outfile;
- {
- int result = SUCCESS;
- long bytes;
-
- bytes = fread(&asd_header, 1, sizeof(asd_hdr), infile);
- if (bytes != sizeof(asd_hdr)) {
- Feedback("Error #%d reading AppleSingle header bytes.", errno);
- result = FAILURE;
- }
- else switch (asd_header.magic) {
- case MAGIC_APPLE_SINGLE: /* AppleSingle */
- Feedback("Apple Single -> Macintosh.");
- result = apple_single_to_mb(inname, infile, outname, outfile, &asd_header);
- break;
- case MAGIC_APPLE_DOUBLE: /* AppleDouble */
- Feedback("Apple Double -> Macintosh.");
- result = apple_double_to_mb(inname, infile, outname, outfile, &asd_header);
- break;
- default:
- Feedback("Unknown magic number 0x%08lx. Not processed.", asd_header.magic);
- result = FAILURE;
- break;
- }
-
- return result;
- }
-
- apple_single_to_mb(inname, infile, outname, outfile, header)
- char *inname;
- FILE *infile;
- char *outname;
- FILE *outfile;
- asd_hdr *header;
- {
- int i;
- int got_data, got_rsrc, got_fndr;
- int i_data, i_rsrc, i_fndr;
- long now;
-
- #pragma unused (outname)
-
- got_data = got_rsrc = got_fndr = 0;
-
- #ifdef TCLAPPL
- print_asd_hdr(header);
- #endif
-
- RotateCursor(32);
- DoYield();
-
- for (i=0; i < header->entries; i++) {
- fread(&the_entries[i], 1, sizeof(asd_entry), infile);
- #ifdef TCLAPPL
- print_asd_entry(&the_entries[i]);
- #endif
- switch (the_entries[i].id) {
- case ASDID_DATA_FORK: got_data = 1; i_data = i; break;
- case ASDID_RSRC_FORK: got_rsrc = 1; i_rsrc = i; break;
- case ASDID_FNDR_FORK: got_fndr = 1; i_fndr = i; break;
- }
- }
-
- mb_header->zero1 = 0;
- mb_header->zero2 = 0;
- mb_header->zero3 = 0;
- mb_header->protected = 0;
-
- strcpy(mb_header->name, inname);
- mb_header->nlen = (unsigned char) strlen(inname);
-
- RotateCursor(32);
- DoYield();
-
- if (! got_fndr) {
- Feedback("NOTE: No Finder Information. Defaulting.");
- strncpy(mb_header->type, "????", 4);
- strncpy(mb_header->creator, "????", 4);
- mb_header->flags = 0;
- memset(mb_header->location, 0, sizeof(mb_header->location));
- }
- else {
- get_asd_fndr_data(infile, &the_entries[i_fndr], &g_asd_finder);
- #ifdef TCLAPPL
- print_asd_fndr(&g_asd_finder);
- #endif
- memcpy(mb_header->type, &g_asd_finder.fdType, 4);
- memcpy(mb_header->creator, &g_asd_finder.fdCreator, 4);
- mb_header->flags = g_asd_finder.fdFlags;
- memcpy(mb_header->location, g_asd_finder.fdLocation,
- sizeof(mb_header->location));
- }
-
- if (got_data) {
- move_long_to_char(the_entries[i_data].length, mb_header->dflen);
- }
- else {
- memset(mb_header->dflen, 0, sizeof(mb_header->dflen));
- }
-
- if (got_rsrc) {
- move_long_to_char(the_entries[i_rsrc].length, mb_header->rflen);
- }
- else {
- memset(mb_header->rflen, 0, sizeof(mb_header->rflen));
- }
-
- now = time(0); now = UNIX_TO_MAC_TIME(now);
- move_long_to_char(now, mb_header->cdate);
- move_long_to_char(now, mb_header->mdate);
-
- RotateCursor(32);
- DoYield();
-
- Feedback("DATA Fork contained %ld bytes.", the_entries[i_data].length);
- Feedback("RSRC Fork contained %ld bytes.", the_entries[i_rsrc].length);
- Feedback("Writing MacBinary header...");
- fwrite(mb_header, 1, MAC_BINARY_HDR_SIZE, outfile);
-
- if (got_data && (the_entries[i_data].length > 0)) {
- Feedback("Writing DATA fork...");
- do_data_copy(infile, the_entries[i_data].offset, outfile, -1, the_entries[i_data].length);
- }
-
- if (got_rsrc && the_entries[i_rsrc].length) {
- Feedback("Writing RSRC fork...");
- do_data_copy(infile, the_entries[i_rsrc].offset, outfile, -1, the_entries[i_rsrc].length);
- }
-
- }
-
- #ifdef TCLAPPL
-
- apple_double_to_mb(rname, rfile, outname, outfile, header)
- char *rname;
- FILE *rfile;
- char *outname;
- FILE *outfile;
- asd_hdr *header;
- {
- FILE *dfile = NULL;
- SFReply myreply;
- Point mypoint;
- SFTypeList mytypes;
- int i;
- int got_data, got_rsrc, got_fndr, got_path;
- int i_data = 0, i_rsrc, i_fndr, i_path;
- long now, bytes;
- char dname[256];
-
- #pragma unused (outname)
-
- got_data = got_rsrc = got_fndr = 0;
-
- #ifdef TCLAPPL
- print_asd_hdr(header);
- #endif
-
- the_entries[i_data].id = ASDID_DATA_FORK;
- the_entries[i_data].offset = 0;
- the_entries[i_data].length = 0;
-
- RotateCursor(32);
- DoYield();
-
- for (i=1; i <= header->entries; i++) {
- fread(&the_entries[i], 1, sizeof(asd_entry), rfile);
- #ifdef TCLAPPL
- print_asd_entry(&the_entries[i]);
- #endif
- switch (the_entries[i].id) {
- case ASDID_RSRC_FORK: got_rsrc = 1; i_rsrc = i; break;
- case ASDID_FNDR_FORK: got_fndr = 1; i_fndr = i; break;
- case ASDID_DATA_PATH: got_path = 1; i_path = i; break;
- }
- }
-
- RotateCursor(32);
- DoYield();
-
- if (got_path) {
- if (the_entries[i_path].length >= sizeof(dname)-1) {
- Feedback("Real Name Too Long: %d > %d. Truncating.",
- the_entries[i_path].length, sizeof(dname)-1);
- the_entries[i_path].length = sizeof(dname) - 1;
- }
-
- bytes = fread(dname, 1, the_entries[i_path].length, rfile);
- if (bytes > the_entries[i_path].length) {
- Feedback("Real Name Too Long: %d > %d. Truncating to '%.*s'.",
- bytes, the_entries[i_path].length, the_entries[i_path].length, dname);
- dname[the_entries[i_path].length] = '\0';
- }
- else if (bytes < the_entries[i_path].length) {
- Feedback("Couldn't Read All Of Real Name: %d < %d. Using '%.*s'.",
- bytes, sizeof(dname)-1, bytes, dname);
- dname[bytes] = '\0';
- }
- else {
- dname[bytes] = '\0';
- }
- Feedback("Opening Real Name: '%s'.", dname);
- dfile = fopen(dname, "r");
- if (dfile != NULL)
- got_data = 1;
- }
-
- RotateCursor(32);
- DoYield();
-
- if (! got_data) {
- if (rname[0] == '%')
- strcpy(dname, &rname[1]);
- else {
- sprintf(dname, "%%%s", rname);
- }
- Feedback("Trying Data Name: '%s'.", dname);
- dfile = fopen(dname, "r");
- if (dfile == NULL) {
- mypoint.h = mypoint.v = 75;
- MyGetFile(mypoint, "\pWhere is data fork?", NULL, -1, mytypes, NULL, &myreply);
- if (myreply.good) {
- sprintf(dname, "%.*s", myreply.fName[0], &myreply.fName[1]);
- SetVol(NULL, myreply.vRefNum);
- dfile = fopen(dname, "r");
- if (dfile != NULL)
- got_data = 1;
- else
- return FAILURE;
- }
- else {
- if (! AskYesNo("Do you just want an empty data fork?", 1))
- return CANCEL;
- }
- }
- else
- got_data = 1;
- }
-
- RotateCursor(32);
- DoYield();
-
- if (dfile != NULL) {
- the_entries[i_data].offset = 0;
- fseek(dfile, 0, SEEK_END);
- the_entries[i_data].length = ftell(dfile);
- fseek(dfile, 0, SEEK_SET);
- }
- else {
- the_entries[i_data].offset = 0;
- the_entries[i_data].length = 0;
- }
- move_long_to_char(the_entries[i_data].length, mb_header->dflen);
-
- mb_header->zero1 = 0;
- mb_header->zero2 = 0;
- mb_header->zero3 = 0;
- mb_header->protected = 0;
-
- strcpy(mb_header->name, rname);
- mb_header->nlen = (unsigned char) strlen(rname);
-
- RotateCursor(32);
- DoYield();
-
- if (! got_fndr) {
- Feedback("NOTE: No Finder Information. Defaulting.");
- strncpy(mb_header->type, "????", 4);
- strncpy(mb_header->creator, "????", 4);
- mb_header->flags = 0;
- memset(mb_header->location, 0, sizeof(mb_header->location));
- }
- else {
- get_asd_fndr_data(rfile, &the_entries[i_fndr], &g_asd_finder);
- #ifdef TCLAPPL
- print_asd_fndr(&g_asd_finder);
- #endif
- memcpy(mb_header->type, &g_asd_finder.fdType, 4);
- memcpy(mb_header->creator, &g_asd_finder.fdCreator, 4);
- mb_header->flags = g_asd_finder.fdFlags;
- memcpy(mb_header->location, g_asd_finder.fdLocation,
- sizeof(mb_header->location));
- }
-
- if (got_rsrc) {
- move_long_to_char(the_entries[i_rsrc].length, mb_header->rflen);
- }
- else {
- memset(mb_header->rflen, 0, sizeof(mb_header->rflen));
- }
-
- now = time(0); now = UNIX_TO_MAC_TIME(now);
- move_long_to_char(now, mb_header->cdate);
- move_long_to_char(now, mb_header->mdate);
-
- RotateCursor(32);
- DoYield();
-
- Feedback("DATA Fork contained %ld bytes.", the_entries[i_data].length);
- Feedback("RSRC Fork contained %ld bytes.", the_entries[i_rsrc].length);
- Feedback("Writing MacBinary header...");
- fwrite(mb_header, 1, MAC_BINARY_HDR_SIZE, outfile);
-
- if (the_entries[i_data].length > 0) {
- Feedback("Writing DATA fork. Off %ld Len %ld dfile = x%08lX ",
- ftell(outfile), the_entries[i_data].length, dfile);
- do_data_copy(dfile, the_entries[i_data].offset,
- outfile, (long)-1, the_entries[i_data].length);
- }
-
- if (got_rsrc && the_entries[i_rsrc].length > 0) {
- Feedback("Writing RSRC fork. Off %ld Len %ld rfile = x%08lX ",
- ftell(outfile), the_entries[i_rsrc].length, rfile);
- do_data_copy(rfile, the_entries[i_rsrc].offset,
- outfile, -1, the_entries[i_rsrc].length);
- }
-
- if (dfile) {
- WatchCursorOn();
- fclose(dfile);
- }
- }
-
- #endif /* TCLAPPL */
-
-
- move_long_to_char(lng, ch)
- long lng;
- char *ch;
- {
- ch[0] = (unsigned char) ((lng >> 24) & 0x000000FF);
- ch[1] = (unsigned char) ((lng >> 16) & 0x000000FF);
- ch[2] = (unsigned char) ((lng >> 8) & 0x000000FF);
- ch[3] = (unsigned char) (lng & 0x000000FF);
- }
-
- do_data_copy(infile, inoffset, outfile, outoffset, length)
- FILE *infile;
- long inoffset;
- FILE *outfile;
- long outoffset;
- long length;
- {
- int blocks, residue;
-
- Feedback("do_data_copy: in_offset = %ld out_offset = %ld ",
- inoffset, outoffset);
-
- if (inoffset != -1)
- fseek(infile, inoffset, SEEK_SET);
- if (outoffset != -1)
- fseek(outfile, outoffset, SEEK_SET);
-
- Feedback("do_data_copy: length = %ld BLKSZ %d ", length, BLK_SIZE);
- blocks = length / BLK_SIZE;
- residue = length % BLK_SIZE;
-
- Feedback("do_data_copy: blks = %ld res %ld", blocks, residue);
- for ( ; blocks ; blocks--) {
- RotateCursor(32);
- DoYield();
- if (pause_op)
- while (pause_op)
- pausing();
-
- if (cancel_current_op)
- return CANCEL;
-
- fread(iobuffer, 1, BLK_SIZE, infile);
- fwrite(iobuffer, 1, BLK_SIZE, outfile);
- }
-
- if (residue) {
- RotateCursor(32);
- DoYield();
-
- memset(iobuffer, 0, BLK_SIZE);
- fread(iobuffer, 1, residue, infile);
- fwrite(iobuffer, 1, BLK_SIZE, outfile);
- }
- }
-
-